package com.hjli.controller;

import java.util.HashMap;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.hjli.tool.PropertiesTool;

@Controller
@RequestMapping("sys/set")
public class SetController {

	/**
	 * 修改基本properties配置
	 * 
	 * @param request
	 * @param response
	 * @return
	 * @throws Exception
	 */
	@RequestMapping("save/web")
	public @ResponseBody Map<String, Object> web(HttpServletRequest request, HttpServletResponse response)
			throws Exception {
		Map<String, Object> resultMap = new HashMap<String, Object>();
		String webname = request.getParameter("webname");
		String keywords = request.getParameter("keywords");
		String description = request.getParameter("description");
		String copyright = request.getParameter("copyright");
		String propPath = request.getSession().getServletContext().getRealPath("WEB-INF/classes/web.properties");

		if (PropertiesTool.updateProperty(propPath, "webname", webname) == true
				&& PropertiesTool.updateProperty(propPath, "keywords", keywords) == true
				&& PropertiesTool.updateProperty(propPath, "description", description) == true
				&& PropertiesTool.updateProperty(propPath, "copyright", copyright) == true) {
			resultMap.put("result", "success");
		}else{
			resultMap.put("result", "修改失败");
		}
	
		return resultMap;
	}
	
	/**
	 * 修改七牛云properties配置
	 * 
	 * @param request
	 * @param response
	 * @return
	 * @throws Exception
	 */
	@RequestMapping("save/qiniu")
	public @ResponseBody Map<String, Object> qiniu(HttpServletRequest request, HttpServletResponse response)
			throws Exception {
		Map<String, Object> resultMap = new HashMap<String, Object>();
		String url = request.getParameter("URL");
		String ak = request.getParameter("AK");
		String sk = request.getParameter("SK");
		String bn = request.getParameter("BN");
		String propPath = request.getSession().getServletContext().getRealPath("WEB-INF/classes/qiniu.properties");

		if (PropertiesTool.updateProperty(propPath, "URL", url) == true
				&& PropertiesTool.updateProperty(propPath, "AK", ak) == true
				&& PropertiesTool.updateProperty(propPath, "SK", sk) == true
				&& PropertiesTool.updateProperty(propPath, "BN", bn) == true) {
			resultMap.put("result", "success");
		}else{
			resultMap.put("result", "修改失败");
		}
	
		return resultMap;
	}
	
	/**
	 * 修改邮件properties配置
	 * 
	 * @param request
	 * @param response
	 * @return
	 * @throws Exception
	 */
	@RequestMapping("save/email")
	public @ResponseBody Map<String, Object> email(HttpServletRequest request, HttpServletResponse response)
			throws Exception {
		Map<String, Object> resultMap = new HashMap<String, Object>();
		String auth = request.getParameter("auth");
		String host = request.getParameter("host");
		String port = request.getParameter("port");
		String user = request.getParameter("user");
		String password = request.getParameter("password");
		String propPath = request.getSession().getServletContext().getRealPath("WEB-INF/classes/email.properties");

		if (PropertiesTool.updateProperty(propPath, "mail.smtp.auth", auth) == true
				&& PropertiesTool.updateProperty(propPath, "mail.smtp.host", host) == true
				&& PropertiesTool.updateProperty(propPath, "mail.smtp.port", port) == true
				&& PropertiesTool.updateProperty(propPath, "mail.user", user) == true
				&& PropertiesTool.updateProperty(propPath, "mail.password", password) == true) {
			resultMap.put("result", "success");
		}else{
			resultMap.put("result", "修改失败");
		}
	
		return resultMap;
	}
	
	/**
	 * 修改图灵properties配置
	 * @param request
	 * @param response
	 * @return
	 * @throws Exception
	 */
	@RequestMapping("save/turing")
	public @ResponseBody Map<String, Object> turing(HttpServletRequest request, HttpServletResponse response)
			throws Exception {
		Map<String, Object> resultMap = new HashMap<String, Object>();
		String apikey = request.getParameter("apikey");
		String secret = request.getParameter("secret");
		String propPath = request.getSession().getServletContext().getRealPath("WEB-INF/classes/tuling.properties");

		if (PropertiesTool.updateProperty(propPath, "apikey", apikey) == true
				&& PropertiesTool.updateProperty(propPath, "secret", secret) == true) {
			resultMap.put("result", "success");
		}else{
			resultMap.put("result", "修改失败");
		}
		return resultMap;
	}
}
